import { onLoad } from '@dcloudio/uni-app'
import { onMounted, ref, watch,getCurrentInstance, provide } from 'vue'
import { objectAny } from '../../../options/base'
import { baseCopy, goToBack, showModal, togglePackStatus } from '../../../utils/utils'
import riderActionBar from '../../components/rider-action-bar/rider-action-bar.vue'
import { db_riderQueryOrderList } from '../../db/order-manage'
import Big from 'big.js';
import moment from 'moment'
import { buildTabListModule, statusToTabList } from '../../utils'
import riderCommonPopup from '../../components/rider-common-popup/rider-common-popup.vue'
import { buildFullBottomPopup } from '../../utils/common-popup';
import {  runOrderCodeConvertView } from '../../utils/status-toggle'

export default {
    components:{
        riderActionBar,
        riderCommonPopup
    },
    setup() {
        const bottomPopupArgs = buildFullBottomPopup();
        for(let key in bottomPopupArgs){
            //@ts-ignore
            provide(key,bottomPopupArgs[key])
        }


        // 当前订单的订单编号 数据库字段 order_id
        const currentOrderId = ref(``);
        // 底部滑块菜单，订单在以下状态时才可以显示
        const sliderMenuInStatus = ref([0,1,2,3])
        // 订单对应的菜单对象
        const orderToTabItem = ref({});
        const serverData = ref<objectAny>({})
        const tabListModule = ref(buildTabListModule([
            getOrderDetails,
            getOrderDetails,
            getOrderDetails
        ]));

        onLoad(async (options) => {
            const orderId = options?.orderId || ''
            if(orderId==''){
                await showModal({
                    title:`异常提示`,
                    content:`非法访问，请联系客服`,
                    showCancel:false
                })
                return goToBack()
            }
            currentOrderId.value=orderId
            getOrderDetails();
        })
        async function getOrderDetails(){
            const orderId = currentOrderId.value;
            const list = await db_riderQueryOrderList(undefined,orderId)
            if(list.length===0){
                await showModal({
                    title:`异常提示`,
                    content:`订单号不存在，请联系客服`,
                    showCancel:false
                })
                return goToBack()
            }
            const orderData = list[0];
            let priceTotal = 0 //合计金额
            if(orderData.moduleName === `foodTakeout`){
                // 计算合计金额
                const checkResultList =  orderData.product_list[0].check_result
                for(let i=0;i<checkResultList.length;i++){
                    const item  = checkResultList[i]
                    const other_price=computedTotalMoeny(item,`product_base_real_price`)
                    priceTotal = +new Big(priceTotal).plus(other_price)
                }
            }else{
                //  其他模块
                priceTotal=runOrderCodeConvertView(orderData)
            }
            // 计算出最总的合计金额
            orderData.priceTotal=priceTotal
            serverData.value = orderData
            orderToTabItem.value=statusToTabList(tabListModule.value,orderData.order_status);

            console.log(
                JSON.parse(
                    JSON.stringify(
                        list[0]
                    )
                )
            )
        }
                
        function computedTotalMoeny(
            checkProduct:objectAny,
            key:'product_base_price'|'product_base_real_price'
        ):number{
            const {
                checkProductInventory,
                count,
                inventoryCheckIds:checkIds,
                product:productServer
            }=checkProduct
            // 找出每个产品规格下的附加金额 并统计 附加金额不打折
            const singleTotal=checkProductInventory.reduce((tot:number,it:objectAny)=>{
                const ctot=it.price_format
                return +new Big(tot).plus(ctot) 
            },0)
        
            return +new Big(productServer[key]).plus(singleTotal).times(count)
        }

        function getMoment(date:string){
            return moment(date).format(`YYYY-MM-DD HH:mm:ss`)
        }
        function getBigJs(args:string){
            return new Big(args)
        }

        return {
            sliderMenuInStatus,
            tabListModule,
            serverData,
            orderToTabItem,
            baseCopy,
            getMoment,
            getOrderDetails,
            computedTotalMoeny,
            statusToTabList,
            getBigJs,
        }

    },
}